<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/5/22
 * Time: 14:49
 */

namespace Home\Service;


use Common\Common\Service;
use Home\Component\ApplyComponent;
use Home\Component\PictureComponent;
use Home\Exception\HomeException;
use Home\Request\Apply\FindApplyRequest;
use Home\Response\FindApplyResponse;

class FindApplyService implements Service
{
    public function run(FindApplyRequest $request = null)
    {
        if ($request->issetApplyId() == false)
            throw new HomeException(10001);
        if ($request->verifyApplyId() == false)
            throw new HomeException(10002);
        if ($request->IsApplyIdInRange() == false)
            throw new HomeException(10003);

        $apply_id = $request->getApplyId();
        $component = new ApplyComponent();
        $response = new FindApplyResponse();

        try {
            $data = $component->findApply($apply_id);
            if (is_null($data) == true) {
                $response->setItIsNull();
            } else {
                    $picture_id = explode(',', $data['picture_id']);
                    $component = new PictureComponent();
                    $pictures = array();
                    for ($j = 0; $j < count($picture_id); $j++) {
                        $picture = $component->getPicture($picture_id[$j]);
                        $pictures[] = $picture['path'];
                    }
                    $data['pictures'] = $pictures;
                $response->setData($data);
            }
        } catch (HomeException $e) {
            throw new HomeException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}